﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cocos2d_xna2.Classes;
using cocos2d;

namespace cocos2d_xna2.Classes
{
    public class ControlLayer : CCLayer
    {
        GameLayer glayer;
        mapAction kAct;

        tankAction tAct;

        // action button
        CCSprite item_f_n, item_f_s, item_u_out, item_u_on, item_l_out, item_l_on, item_r_out, item_r_on, item_d_out, item_d_on;

        public GameLayer Glayer
        {
            get
            {
                return glayer;
            }
            set
            {
                glayer = value;
            }
        }



        public static new ControlLayer node()
        {
            ControlLayer layer = new ControlLayer();
            if (layer.init())
                return layer;
            return null;
        }

        public override bool init()
        {
            if (!base.init())
            {
                return false;
            }

            CCSpriteBatchNode mgr = CCSpriteBatchNode.batchNodeWithFile(@"images/JoyStickMenu");
            addChild(mgr);


            item_f_n = CCSprite.spriteWithTexture(mgr.Texture, ccMacros.CCRectMake((2 + 0) * 11, 0, 11, 11));
            item_f_n.position = CCPointExtension.ccp(0, 0);
            mgr.addChild(item_f_n);
            item_f_s = CCSprite.spriteWithTexture(mgr.Texture, ccMacros.CCRectMake((2 + 5) * 11, 0, 11, 11));
            item_f_s.position = CCPointExtension.ccp(0, 0);
            mgr.addChild(item_f_s);


            //UP
            item_u_on = CCSprite.spriteWithTexture(mgr.Texture, ccMacros.CCRectMake((0 + 0) * 11, 0, 11, 11));
            item_u_out = CCSprite.spriteWithTexture(mgr.Texture, ccMacros.CCRectMake((0 + 5) * 11, 0, 11, 11));
            item_u_on.position = CCPointExtension.ccp(0, 11);
            item_u_out.position = CCPointExtension.ccp(0, 11);
            mgr.addChild(item_u_on);
            mgr.addChild(item_u_out);


            //DOWN
            item_d_on = CCSprite.spriteWithTexture(mgr.Texture, ccMacros.CCRectMake((4 + 0) * 11, 0, 11, 11));
            item_d_out = CCSprite.spriteWithTexture(mgr.Texture, ccMacros.CCRectMake((4 + 5) * 11, 0, 11, 11));
            item_d_on.position = CCPointExtension.ccp(0, -11);
            item_d_out.position = CCPointExtension.ccp(0, -11);
            mgr.addChild(item_d_on);
            mgr.addChild(item_d_out);



            //Left
            item_l_on = CCSprite.spriteWithTexture(mgr.Texture, ccMacros.CCRectMake((1 + 0) * 11, 0, 11, 11));
            item_l_out = CCSprite.spriteWithTexture(mgr.Texture, ccMacros.CCRectMake((1 + 5) * 11, 0, 11, 11));
            item_l_on.position = CCPointExtension.ccp(-11, 0);
            item_l_out.position = CCPointExtension.ccp(-11, 0);
            mgr.addChild(item_l_on);
            mgr.addChild(item_l_out);


            //Right
            item_r_on = CCSprite.spriteWithTexture(mgr.Texture, ccMacros.CCRectMake((3 + 0) * 11, 0, 11, 11));
            item_r_out = CCSprite.spriteWithTexture(mgr.Texture, ccMacros.CCRectMake((3 + 5) * 11, 0, 11, 11));
            item_r_on.position = CCPointExtension.ccp(11, 0);
            item_r_out.position = CCPointExtension.ccp(11, 0);
            mgr.addChild(item_r_on);
            mgr.addChild(item_r_out);

            item_f_n.visible = true;
            item_f_s.visible = false;

            item_u_on.visible = true;
            item_u_out.visible = false;

            item_d_on.visible = true;
            item_d_out.visible = false;

            item_l_on.visible = true;
            item_l_out.visible = false;

            item_r_on.visible = true;
            item_r_out.visible = false;

            mgr.position = CCPointExtension.ccp(240, 30);
            isTouchEnabled = true;

            return true;
            
        }

        public override void registerWithTouchDispatcher()
        {
            CCTouchDispatcher.sharedDispatcher().addTargetedDelegate(this, int.MinValue+1, true);
            
        }

        CCRect AtlasRect(CCSprite altSpr)
        {
            CCRect crect = altSpr.textureRect;
            return ccMacros.CCRectMake(-crect.size.width / 2, -crect.size.height / 2, crect.size.width, crect.size.height);
        }

        void keepDoing(float dt)
        {
            //glayer.onTankAction(kAct);
            glayer.onTankAction(tAct);
        }

        public override bool ccTouchBegan(CCTouch touch, CCEvent event_)
        {
            CCPoint location = item_f_n.convertTouchToNodeSpaceAR(touch);
            CCRect tempRect = AtlasRect(item_f_n);
            if (CCRect.CCRectContainsPoint(tempRect, location))
            {
                item_f_n.visible = false;
                item_f_s.visible = true;
                tAct = tankAction.tFire;
                schedule(keepDoing, 1/15);
                return true;
            }

            location = item_u_on.convertTouchToNodeSpaceAR(touch);
            tempRect = AtlasRect(item_u_on);
            if (CCRect.CCRectContainsPoint(tempRect, location))
            {
                item_u_on.visible = false;
                item_u_out.visible = true;
                tAct = tankAction.tUp;
                schedule(keepDoing, 1 / 30);
                return true;
            }

            location = item_d_on.convertTouchToNodeSpaceAR(touch);
            tempRect = AtlasRect(item_d_on);
            if (CCRect.CCRectContainsPoint(tempRect, location))
            {
                item_d_on.visible = false;
                item_d_out.visible = true;
                tAct = tankAction.tDown;
                schedule(keepDoing, 1 / 30);
                return true;
            }

            location = item_l_on.convertTouchToNodeSpaceAR(touch);
            tempRect = AtlasRect(item_l_on);
            if (CCRect.CCRectContainsPoint(tempRect, location))
            {
                item_l_on.visible = false;
                item_l_out.visible = true;
                tAct = tankAction.tLeft;
                schedule(keepDoing, 1 / 30);
                return true;
            }


            location = item_r_on.convertTouchToNodeSpaceAR(touch);
            tempRect = AtlasRect(item_r_on);
            if (CCRect.CCRectContainsPoint(tempRect, location))
            {
                item_r_on.visible = false;
                item_r_out.visible = true;
                tAct = tankAction.tRight;
                schedule(keepDoing, 1 / 30);
                return true;
            }
            return false;
        }


        public override void ccTouchMoved(CCTouch touch, CCEvent event_)
        {
            CCPoint location = item_f_n.convertTouchToNodeSpaceAR(touch);
            CCRect tempRect = AtlasRect(item_f_n);
            bool bHit = false;
            mapAction lastAct = kAct;
            if (CCRect.CCRectContainsPoint(tempRect, location))
            {
                item_f_n.visible = false;
                item_f_s.visible = true;
                kAct = mapAction.kUp;
                bHit = true;

            }


            location = item_u_on.convertTouchToNodeSpaceAR(touch);
            tempRect = AtlasRect(item_u_on);
            if (CCRect.CCRectContainsPoint(tempRect, location))
            {
                item_u_on.visible = false;
                item_u_out.visible = true;
                kAct = mapAction.kUp;
                bHit = true;

            }

            location = item_d_on.convertTouchToNodeSpaceAR(touch);
            tempRect = AtlasRect(item_d_on);
            if (CCRect.CCRectContainsPoint(tempRect, location))
            {
                item_d_on.visible = false;
                item_d_out.visible = true;
                kAct = mapAction.kDown;
                bHit = true;

            }

            location = item_l_on.convertTouchToNodeSpaceAR(touch);
            tempRect = AtlasRect(item_l_on);
            if (CCRect.CCRectContainsPoint(tempRect, location))
            {
                item_l_on.visible = false;
                item_l_out.visible = true;
                kAct = mapAction.kLeft;
                bHit = true;

            }


            location = item_r_on.convertTouchToNodeSpaceAR(touch);
            tempRect = AtlasRect(item_r_on);
            if (CCRect.CCRectContainsPoint(tempRect, location))
            {
                item_r_on.visible = false;
                item_r_out.visible = true;
                kAct = mapAction.kRight;
                bHit = true;

            }



            if (!bHit)
            {
                unschedule(keepDoing);
                item_u_on.visible = true;
                item_u_out.visible = false;

                item_d_on.visible = true;
                item_d_out.visible = false;

                item_l_on.visible = true;
                item_l_out.visible = false;

                item_r_on.visible = true;
                item_r_out.visible = false;

                kAct = mapAction.kStay;
            }
            else
            {
                if (lastAct != kAct)
                {
                    schedule(keepDoing, 1 / 30);
                }
            }
        }

        public override void ccTouchEnded(CCTouch touch, CCEvent event_)
        {
            unschedule(keepDoing);
            item_f_n.visible = true;
            item_f_s.visible = false;

            item_u_on.visible = true;
            item_u_out.visible = false;

            item_d_on.visible = true;
            item_d_out.visible = false;

            item_l_on.visible = true;
            item_l_out.visible = false;

            item_r_on.visible = true;
            item_r_out.visible = false;

            kAct = mapAction.kStay;
        }

    }
}
